package it.polimi.digitalstate.webbeans;

import java.util.LinkedList;
import java.util.List;

import it.polimi.digitalstate.dto.JoinrequestDTO;
import it.polimi.digitalstate.dto.PartyDTO;
import it.polimi.digitalstate.manager.AuthenticationManagerLocal;
import it.polimi.digitalstate.manager.MembershipManagerLocal;
import it.polimi.digitalstate.manager.PartyManagerLocal;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
/**
 * This Bean shows the personal page of a citizen:
 * it incorporates different links (that will be shown if there are the right premises)
 * 
 * - Create a party
 * - Join a party
 * - View a party
 *
 */
@ManagedBean(name="personalPageBean")
@SessionScoped
public class PersonalPageBean {
	@EJB
	PartyManagerLocal ejb_partymanager;
	@EJB
	AuthenticationManagerLocal ejb_user;
	@EJB
	MembershipManagerLocal ejb_membership;
	@EJB
	PartyManagerLocal ejb_party;
	
	
	public boolean canCreateParty() {
		System.out.println(ejb_user.getLoggedCitizen().getFullName());
		return ejb_partymanager.canCreateParty(ejb_user.getLoggedCitizen());
	}
	private boolean showNotification(JoinrequestDTO joinrequest){
		return ejb_membership.canJoinVote(ejb_user.getLoggedCitizen(), joinrequest);
	}
	public boolean canRequest() {
		return (ejb_membership.canRequest(ejb_user.getLoggedCitizen()));
	}
	public boolean isMemberOfParty(){
		return (ejb_partymanager.hasParty(ejb_user.getLoggedCitizen()));
	}
	public PartyDTO getParty(){
		return ejb_party.getPartyByCitizen(ejb_user.getLoggedCitizen());
	}
	public JoinrequestDTO[] getRequestList(){
		/*it returns just the right notifications*/
		JoinrequestDTO[] jrlt =  ejb_membership.getJoinrequestFromParty(ejb_user.getLoggedCitizen(), getParty());
		List<JoinrequestDTO> jrlist= new LinkedList<JoinrequestDTO>();
		for(int i=0;i<jrlt.length;i++){
			if(showNotification(jrlt[i]))
				jrlist.add(jrlt[i]);
		}
		JoinrequestDTO[] trasf = new JoinrequestDTO[jrlist.toArray().length];
		for(int i=0;i<jrlist.size();i++)
			trasf[i]=jrlist.get(i);
		return trasf;
	}
	public int getNumberOfNotification(){
		return getRequestList().length;
	}
	public boolean hasPendingRequest(){
		return ejb_membership.isMembershipRequester(ejb_user.getLoggedCitizen());
	}
}
